import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:test01/okhttp_test/page/indexlogin/verification_code_page.dart';
import 'package:test01/okhttp_test/pojo/login_request_pojo.dart';
import 'package:test01/okhttp_test/util/request_http.dart';

/*
*
* 登录界面------手机号码输入
*
*
* */

class TelephonePage extends StatefulWidget {

  final String name;
  TelephonePage({Key key,this.name=""}) : super(key: key);

  @override
  _TelephonePageState createState() => _TelephonePageState(name);
}

class _TelephonePageState extends State<TelephonePage> {
  var _textFieldValue = '';
  var hintText="请输入你的手机号";
  ///是否显示清除按钮
  bool isShowdelete=false;
  ///发送是否显示
  bool isShowSend=false;
  ///实例化控制器
  TextEditingController controller=TextEditingController();
  String name;
  _TelephonePageState(this.name);

  ///判断手机验证码是否通过
  bool isAccessCode=false;

  ///初始化
  @override
  void initState() {
    super.initState();
  }

  ///手机发送验证码接口
  void phoneVerification() async{
    LoginSend loginSend=LoginSend(authorizations:"Basic dXNlckFwcDpBaW15Rml0bmVzcw==",phoneNumber:_textFieldValue,type:0,anonymousToken:tokens);
    isAccessCode=await getloginsend(loginSend, "/uaa/oauth/authentication/sms/send");
    print("-------------------手机状态$isAccessCode-================================");
    if(isAccessCode){
      ///进入提示
      Fluttertoast.showToast(msg:"验证码发送成功！",toastLength: Toast.LENGTH_SHORT,gravity: ToastGravity.BOTTOM,
          textColor: Colors.white,
          fontSize: 14.0
      );
      isShowSend=false;
      ///界面跳转
      Navigator.push(context, MaterialPageRoute(builder: (context) {
        return VerificationCodePage(phoneCode: _textFieldValue,);
      }));
    }else{
      print("-------------------未通过-================================");
      ///进入提示
      Fluttertoast.showToast(msg:"验证码发送失败！重试！",toastLength: Toast.LENGTH_SHORT,gravity: ToastGravity.BOTTOM,
          textColor: Colors.white,
          fontSize: 14.0
      );
      isShowSend=false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
            leading:
            IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.black),onPressed: (){
              ///界面返回
              Navigator.of(context).pop();
              print("返回");
            },),
            backgroundColor: Colors.white,
            elevation: 0,
            brightness: Brightness.light //light是黑字，dark是白字

        ),


        body: Container(
            color: Colors.white,
            ///手机屏宽
            width: MediaQuery.of(context).size.width,
            child:
            Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.fromLTRB(41, 42.5, 0, 0),
                    child: Text(
                      "Hello~",
                      style: TextStyle(fontSize: 23,color: Color.fromRGBO(17, 17, 17, 1)),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.fromLTRB(41, 9, 0, 0),
                    child: Text(
                      "此刻开始分享你的健身生活和经验吧~",
                      style: TextStyle(
                          fontSize: 14,
                          color: Color.fromRGBO(153, 153, 153, 1)
                      ),
                    ),
                  ),
                  Container(
                    width: MediaQuery.of(context).size.width-82 ,
                    margin: EdgeInsets.fromLTRB(41, 38, 0, 0),
                    child:
                    TextField(
                      autofocus: false,
                      controller: controller,
                      decoration:
                      InputDecoration(
                        suffixIcon:
                        ///清除事件单击
                        GestureDetector(
                          onTap:(){
                            controller.text="";
                            isShowdelete=false;
                            setState(() {
                            });
                          },
                          child:
                          isShowdelete?
                          Icon(Icons.add_circle_outline,color: Colors.grey,size: 18):SizedBox(),
                        ),
                        hintText: hintText,
                        hintStyle: TextStyle(color: Color.fromRGBO(153, 153, 153, 1),fontSize: 16),
                        hintMaxLines: 1,
                        ///获取焦点
                        focusedBorder: UnderlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.grey.withOpacity(0.2),
                            )),
                        ///默认状态下
                        enabledBorder: UnderlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.grey.withOpacity(0.2),
                            )),
                      ),
                      ///获取文本框数据
                      onChanged: (value){
                        if(value.length!=0){
                          isShowdelete=true;
                          isShowSend=false;
                        }else{
                          isShowdelete=false;
                          isShowSend=false;
                        }
                        _textFieldValue = value;
                        setState(() {

                        });
                      },
                      ///设置键盘格式
                      keyboardType: TextInputType.phone, //手机号
                    ),
                  ),

                  GestureDetector(
                    ///判断按钮状态
                    onTap: (){
                      if( isChinaPhoneLegal(_textFieldValue)){
                        isShowSend=true;
                        setState(() {

                        });
                        phoneVerification();
                      }else{
                        isShowSend=false;
                        Fluttertoast.showToast(msg:"请输入正确的手机号码",toastLength: Toast.LENGTH_SHORT,gravity: ToastGravity.BOTTOM,
                            textColor: Colors.white,
                            fontSize: 14.0
                        );
                      }
                    },
                    child:Container(
                      height: 44,
                      width: MediaQuery.of(context).size.width-81,
                      margin: EdgeInsets.only(left: 41,top: 32),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: isChinaPhoneLegal(_textFieldValue)?Color.fromRGBO(17, 17, 17, 1):Color.fromRGBO(17, 17, 17, 0.06),
                        borderRadius:
                        BorderRadius.all(Radius.circular(3.0)),
                      ),
                      child:isShowSend?
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Container(
                            width: 17,
                            height: 17,
                            child:CircularProgressIndicator(
                              color: Color.fromRGBO(255, 255, 255, 1),
                              strokeWidth: 2,
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 8.5),
                            child: Text("发送中",style: TextStyle(fontSize: 16,color: Color.fromRGBO(255, 255, 255, 1)),),
                          ),
                        ],
                      ):
                      Text("发送验证码",style: TextStyle(fontSize: 16,color: isChinaPhoneLegal(_textFieldValue)?Color.fromRGBO(255, 255, 255, 1):Color.fromRGBO(153, 153, 153, 1)),),
                    ),
                  ),
                ]
            )
        )
    );
  }
  ///大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
  /// 此方法中前三位格式有：
  /// 13+任意数 * 15+除4的任意数 * 18+除1和4的任意数 * 17+除9的任意数 * 147
  static bool isChinaPhoneLegal(String str) {
    return new RegExp('^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$').hasMatch(str);
  }
}